<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'"
               :close-on-click-modal="false"
               :visible.sync="visible"
    width="600px">
        <el-form :model="dataForm"
                 :rules="dataRule"
                 ref="dataForm"
                 @keyup.enter.native="dataFormSubmit()"
                 label-width="110px">
           
            <el-form-item label="关怀描述"
                          prop="careType">
                <el-input v-model="dataForm.careType" placeholder="请输入关怀描述" clearable maxlength="20" show-word-limit></el-input>
<!--                <el-select v-model="dataForm.careType" placeholder="请选择">-->
<!--                    <el-option-->
<!--                        v-for="item in ghTypeList"-->
<!--                        :key="item.value"-->
<!--                        :label="item.label"-->
<!--                        :value="item.value">-->
<!--                    </el-option>-->
<!--                </el-select>-->
            </el-form-item>
            <el-form-item label="是否全部参加"
                          prop="isAllcust" v-if="userRoles.includes('1')">
              <el-radio-group v-model="dataForm.isAllcust" placeholder="请选择">
                <el-radio label="1">是</el-radio>
                <el-radio label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="参加客户" prop="custInfoList" v-if="dataForm.isAllcust === '0'">
                <el-select v-model="dataForm.custInfoList"
                           filterable
                           multiple
                           placeholder="请选择">
                    <el-option v-for="cust in custList"
                               :key="cust.id"
                               :label="cust.custName"
                               :value="cust.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="负责人"
                          prop="chargePerson">
                <el-select v-model="dataForm.chargePerson" placeholder="请选择">
                    <el-option
                        v-for="item in personList"
                        :key="item.empNo"
                        :label="item.empName"
                        :value="item.empNo">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="开始日期" prop="begDate">
                <el-date-picker v-model="dataForm.begDate"
                                type="date"
                                value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期" prop="endDate">
                <el-date-picker v-model="dataForm.endDate"
                                type="date"
                                value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer"
              class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
import { Debounce } from '@/utils/debounce'
import {ghTypeList} from '@/utils/data'
export default {
	data () {
		return {
			visible: false,
            ghTypeList: ghTypeList,
            custList: [],
            personList: [],
            userRoles: sessionStorage.userRoles,
			dataForm: {
                careType: "",
                isAllcust: "0",
                isEnable: "1",
                begDate: "",
                endDate: "",
                chargePerson: "",
                biz1: "",
                custInfoList: []
			},
			dataRule: {
                careType: [
                    { required: true, message: '请选择关怀描述', trigger: 'blur' }
                ],
                isAllcust: [
                    { required: true, message: '请选择是否全部参加', trigger: 'change' }
                ],
                custInfoList: [
                    { required: true, message: '请选择客户', trigger: 'change' }
                ],
                chargePerson: [
                    { required: true, message: '请选择负责人', trigger: 'change' }
                ],
                begDate: [
                    { required: true, message: '请选择开始日期', trigger: 'change' }
                ],
                endDate: [
                    { required: true, message: '请选择结束日期', trigger: 'change' }
                ],
            }
		}
	},
	methods: {
		init (id) {
			this.dataForm.id = id || ""
			this.visible = true
            this.getPersonList()
            this.getCustList()
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields()
				if (this.dataForm.id) {
					this.$http({
						url: this.$http.adornUrl('/crm/base/cusRule/info/' + this.dataForm.id),
						method: 'get',
						params: this.$http.adornParams()
					}).then(({ data }) => {
						this.dataForm = data
                        if(this.dataForm.isAllcust === "0"){
                            this.getAddCust()
                        }
					})
				}else{
                    this.dataForm = {
                        careType: "",
                        isAllcust: "0",
                        isEnable: "1",
                        biz1: "",
                        begDate: "",
                        endDate: "",
                        chargePerson: "",
                        custInfoList: []
                    }
                }
			})
		},
        getAddCust(){
            this.$http({
                url: this.$http.adornUrl('/crm/base/cusRule/getCustsByCare'),
                method: 'post',
                data: this.$http.adornData({
                    id: this.dataForm.id
                })
            }).then(({ data }) => {
                this.dataForm.custInfoList = []
                data.obj.forEach((item) => {
                    this.dataForm.custInfoList.push(item.id)
                })
            })
        },
        getPersonList(){
            this.$http({
                url: this.$http.adornUrl('/crm/base/emp/page'),
                method: 'get',
                params: this.$http.adornParams(Object.assign({
                    current: 1,
                    size: 999
                }))
            }).then(({data}) => {
                this.personList = data.obj.records;
            })
        },
       getCustList(){
           this.$http({
               url: this.$http.adornUrl('/crm/base/cust/page'),
               method: 'get',
               params: this.$http.adornParams(
                   Object.assign(
                       {
                           current: 1,
                           size: 999
                       },
                   )
               )
           }).then(({data}) => {
               this.custList = data.obj.records
               this.custList.forEach((item) => {
                   item.custName = item.custName + "-" + item.custManName
               })
           })
       },
		// 表单提交
		dataFormSubmit: Debounce(function () {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
                    let person = this.personList.find((p) => {
                        return p.empNo === this.dataForm.chargePerson
                    })
					this.$http({
						url: this.$http.adornUrl(this.dataForm.id ? '/crm/base/cusRule/updCustCareRule' : '/crm/base/cusRule/saveCustCareRule'),
						method: 'post',
						data: this.$http.adornData({
                            ...this.dataForm,
                            biz1: person.empName,
                            id: this.dataForm.id ? this.dataForm.id : wfy.uuid(32)
                        })
					}).then(({ data }) => {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.visible = false
								this.$emit('refreshDataList')
							}
						})
					})
				}
			})
		})
	}
}
</script>
